package com.xxxx.server.controller;


import com.xxxx.server.mq.send.SendMailManager;
import com.xxxx.server.pojo.*;
import com.xxxx.server.query.EmpQuery;
import com.xxxx.server.service.impl.*;
import com.xxxx.server.utils.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-09-07
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeServiceImpl employeeService;
    @Resource
    private PositionServiceImpl positionService;
    @Resource
    private NationServiceImpl nationService;
    @Resource
    private JoblevelServiceImpl joblevelService;
    @Resource
    private PoliticsStatusServiceImpl politicsStatusService;
    @Resource
    private DepartmentServiceImpl departmentService;
    @Autowired
    private SendMailManager sendMailManager;

    /**
     * 多条件分页查询
     * @param empQuery
     * @return
     */
    @ApiOperation(value = "获取所有员工")
    @GetMapping("/basic")
    @ResponseBody
    public Map<String, Object> selectEmpByParams(EmpQuery empQuery, HttpServletRequest request) {
        System.out.println(request.getMethod());
        return employeeService.selectEmpByParams(empQuery,request);
    }

    @ApiOperation(value = "添加员工")
    @PostMapping("/basic")
    @ResponseBody
    public Integer insertEmp(@RequestBody Employee employee) {
        //--------邮件发送-----------
        final String email = employee.getEmail();
        new Thread(() -> sendMailManager.sendSimpleMail(email)).start();
        //----------------------------
        return employeeService.insertEmp(employee);
    }

    @ApiOperation(value = "编辑员工")
    @PutMapping("/basic")
    @ResponseBody
    public Integer updateEmp(@RequestBody Employee employee) {
        return employeeService.updateEmp(employee);
    }

    @ApiOperation(value = "删除员工")
    @DeleteMapping("/basic/{id}")
    @ResponseBody
    public Integer deleteEmp(@PathVariable Integer id) {
        return employeeService.deleteEmp(id);
    }

    @ApiOperation(value = "获取职位")
    @RequestMapping("/basic/positions")
    @ResponseBody
    public List<Position> selectPosition() {
        return positionService.selectPosition();
    }

    @ApiOperation(value = "获取民族")
    @RequestMapping("/basic/nations")
    @ResponseBody
    public List<Nation> selectNation() {
        return nationService.selectNation();
    }

    @ApiOperation(value = "获取职位等级")
    @RequestMapping("/basic/joblevels")
    @ResponseBody
    public List<Joblevel> selectJoblevel() {
        return joblevelService.selectJoblevel();
    }

    @ApiOperation(value = "获取政治面貌")
    @RequestMapping("/basic/politicsstatus")
    @ResponseBody
    public List<PoliticsStatus> selectpoliticsStatus() {
        return politicsStatusService.selectpoliticsStatus();
    }

    @ApiOperation(value = "获取部门")
    @RequestMapping("/basic/deps")
    @ResponseBody
    public List<Department> selectDepartmentStatus() {
        return departmentService.selectDepartmentStatus();
    }

    @ApiOperation(value = "获取工号")
    @RequestMapping("/basic/maxWorkID")
    @ResponseBody
    public Map<String, Object> selectEmpMaxWorkId() {
        return employeeService.selectEmpMaxWorkId();
    }

    @ApiOperation(value = "导出员工信息数据")
    @GetMapping("/basic/export")
    public void excelExport(HttpServletResponse response) throws IOException {
        final List<Employee> list = employeeService.list();
        ExcelUtils.exportExcel(list, "员工信息表", "员工信息",
                Employee.class, "员工信息", response);
    }

    @ApiOperation(value = "导入员工信息数据")
    @PostMapping("/basic/import")
    public void excelImport(@RequestParam("file") MultipartFile file) throws IOException {
        final List<Employee> list = ExcelUtils.importExcel(file, Employee.class);
        employeeService.saveBatch(list);
    }
}
